package com.panda.system.service.impl;

import com.panda.common.utils.DateUtils;
import com.panda.system.common.constant.RedisKeyConstants;
import com.panda.system.domain.PandaStore;
import com.panda.system.dto.TBauthModel;
import com.panda.system.mapper.PandaStoreMapper;
import com.panda.system.service.IPandaStoreService;
import com.panda.system.service.ISysConfigService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.List;

/**
 * 店铺信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-01-02
 */
@Service
public class PandaStoreServiceImpl implements IPandaStoreService 
{
    @Autowired
    private PandaStoreMapper pandaStoreMapper;

    @Autowired
    private ISysConfigService sysConfigService;

    /**
     * 查询店铺信息
     * 
     * @param storeId 店铺信息ID
     * @return 店铺信息
     */
    @Override
    public PandaStore selectPandaStoreById(Long storeId)
    {
        return pandaStoreMapper.selectPandaStoreById(storeId);
    }

    /**
     * 查询店铺信息列表
     * 
     * @param pandaStore 店铺信息
     * @return 店铺信息
     */
    @Override
    public List<PandaStore> selectPandaStoreList(PandaStore pandaStore)
    {
        return pandaStoreMapper.selectPandaStoreList(pandaStore);
    }

    /**
     * 新增店铺信息
     * 
     * @param pandaStore 店铺信息
     * @return 结果
     */
    @Override
    public int insertPandaStore(PandaStore pandaStore)
    {
        pandaStore.setCreateTime(DateUtils.getNowDate());
        return pandaStoreMapper.insertPandaStore(pandaStore);
    }

    /**
     * 修改店铺信息
     * 
     * @param pandaStore 店铺信息
     * @return 结果
     */
    @Override
    public int updatePandaStore(PandaStore pandaStore)
    {
        // 获取原信息
        if (Strings.isNotEmpty(pandaStore.getSecret())&&pandaStore.getSecret().length()>32) {
            PandaStore oldStore = pandaStoreMapper.selectPandaStoreById(pandaStore.getStoreId());
            if(!pandaStore.getSecret().equals(oldStore)){
                // 设置过期时间 默认一年
                Calendar calendar = Calendar.getInstance();
                calendar.add(Calendar.YEAR, 1);
                pandaStore.setFailureTime(calendar.getTime());
            }
        }
        pandaStore.setUpdateTime(DateUtils.getNowDate());
        return pandaStoreMapper.updatePandaStore(pandaStore);
    }

    /**
     * 批量删除店铺信息
     * 
     * @param storeIds 需要删除的店铺信息ID
     * @return 结果
     */
    @Override
    public int deletePandaStoreByIds(Long[] storeIds)
    {
        return pandaStoreMapper.deletePandaStoreByIds(storeIds);
    }

    /**
     * 删除店铺信息信息
     * 
     * @param storeId 店铺信息ID
     * @return 结果
     */
    @Override
    public int deletePandaStoreById(Long storeId)
    {
        return pandaStoreMapper.deletePandaStoreById(storeId);
    }

    @Override
    public List<PandaStore> selectPandaStoreListByNames(List<String> storeNames) {
        return pandaStoreMapper.selectPandaStoreListByNames(storeNames);
    }

    @Override
    public TBauthModel querySystemConfig() {
        TBauthModel authModel = new TBauthModel();
        authModel.setUrl(sysConfigService.selectConfigByKey(RedisKeyConstants.TAOBAO_URL));
        authModel.setAppKey(sysConfigService.selectConfigByKey(RedisKeyConstants.TAOBAO_APPKEY));
        authModel.setAppSecret(sysConfigService.selectConfigByKey(RedisKeyConstants.TAOBAO_APPSECRET));
        return authModel;
    }
}
